import java.util.*;


//理解集合框架
public class TestDemo {
    public static void main1(String[] args) {
//        Collection接口 不能new 接口   可以new   ArrayList  它是该接口下具体的实现类
        Collection<String> collection = new ArrayList<>();
        collection.add("hello");
        collection.add("world");
//        collection.add(12);error  因为<String>内是该类型
        System.out.println(collection);//[hello, world]
        // 这里是接口类型不应该打印地址吗?
//        因为ArrayList父类的父类 有重写toString方法   Ctrl+鼠标左键进入该类 --》Alt+7 出现Structure面板--》Ctrl+f查找


//      1.
//        System.out.println(collection.size());//2
//        System.out.println("============");
//        collection.clear();//清空
//        System.out.println(collection);//[]
//        System.out.println(collection.isEmpty());//true

//      2.
//        Object[] objects = collection.toArray();//集合变成数组然后返回出来  点到toArray看到源码知道放回类型是objects
//        System.out.println(Arrays.toString(objects));//[hello, world]


    }


    public static void main2(String[] args) {
        //        Collection<int> collection1 = new ArrayList<>();error 因为<>里面内是类类型 不能是基本数据类型
        Collection<Integer> collection1 = new ArrayList<>();
        collection1.add(12);
        collection1.add(23);
        collection1.add(34);
    }


    public static void main3(String[] args) {
        Map<String,String> map1 = new HashMap<>();
        map1.put("猫哥","cat");
        map1.put("狗哥","dog");
        String ret = map1.get("猫哥");
        System.out.println(ret);//cat
        String ret2 = map1.get("猪哥");
        System.out.println(ret2);//null

        String ret3 = map1.getOrDefault("猪哥","animal");
        String ret4 = map1.getOrDefault("狗哥","animal");
        System.out.println(ret3);//animal
        System.out.println(ret4);//dog

        boolean flg = map1.containsKey("猫哥");
        System.out.println(flg);//true
    }

    public static void main4(String[] args) {
        Map<String,String> map = new HashMap<>();
        map.put("猫哥","cat");
        map.put("狗哥","dog");
        System.out.println(map);//{狗哥=dog, 猫哥=cat}

        System.out.println("=====================");
        Set<Map.Entry<String,String>> entries = map.entrySet();
        for (Map.Entry<String,String> entry: entries) {
            System.out.println("key:"+entry.getKey()+" value:"+entry.getValue());
        }
//          key:狗哥 value:dog
//          key:猫哥 value:cat
    }

    public static void main(String[] args) {
        Map<String,String> map = new TreeMap<>();
        map.put("猫哥","cat");
        map.put("狗哥","dog");
        map.put("猪","pig");
        System.out.println(map);//{狗哥=dog, 猪=pig, 猫哥=cat}

        Map<String,String> map2 = new HashMap<>();
        map2.put("猫哥","cat");
        map2.put("狗哥","dog");
        map2.put("猪","pig");
        System.out.println(map2);//{狗哥=dog, 猫哥=cat, 猪=pig}

    }












}
